package com.mdl.springmvc.handler.handlers;

import com.mdl.springmvc.handler.dto.GaiLouDto;
import com.mdl.springmvc.handler.entity.Gailou;
import com.mdl.springmvc.handler.service.GaiLouService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Desc
 * @Author madengling
 * @Time 2020/11/4 14:07
 */
@Controller
@RequestMapping("/gailou")
public class GaiLouHandler {
    private static Logger logger = Logger.getLogger(GaiLouHandler.class);
    @Autowired
    private GaiLouService gaiLouService;

    @RequestMapping("/get_by_id")
    public String getById(Integer id, Map<String,Object> map){
        Gailou gailou = gaiLouService.getGaiLouById(id);
        map.put("zhuli",gailou);
        return "zhuliList";
    }

    @RequestMapping("/get_all")
    public String getById(Map<String,Object> map){
        List<Gailou> gailouList = gaiLouService.getGaiLouAll();
        map.put("zhulis",gailouList);
        return "zhuliList";
    }

    @RequestMapping("/yuliu")
    public void yuliu(HttpServletResponse response, GaiLouDto dto) throws IOException {
        String message = "";
        Gailou gailou = gaiLouService.getGaiLouById(dto.getId());
        try {
            if(dto.getDengji()!=null || dto.getCishu()!=null){
                gaiLouService.editYuLiuMessage(gailou,dto.getDengji(),dto.getCishu(),dto.getYuliuMessage());
            }else {
                gaiLouService.putYuLiuMessage(gailou,dto.getYuliuMessage());
            }
        }catch (RuntimeException e){
            message = e.getMessage();
        }
        logger.info(message);
        if(StringUtils.isEmpty(message)){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
    }

    @RequestMapping("/zhulijian")
    public void zhulijian(HttpServletResponse response, Integer id) throws IOException {
        String message = "";
        Gailou gailou = gaiLouService.getGaiLouById(id);
        try {
            gaiLouService.putZhuliJian(gailou);
        }catch (RuntimeException e){
            message = e.getMessage();
        }
        logger.info(message);
        if(StringUtils.isEmpty(message)){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
    }

    @RequestMapping("/qingchu")
    public void qingchu(HttpServletResponse response) throws IOException {
        String message = "";
        List<Gailou> gailouList = gaiLouService.getGaiLouAll();
        try {
            for(Gailou gailou : gailouList) {
                gaiLouService.qingchuZhuliJian(gailou);
            }
        }catch (RuntimeException e){
            message = e.getMessage();
        }
        logger.info(message);
        if(StringUtils.isEmpty(message)){
            response.getWriter().print(true);
        }else {
            response.getWriter().print(false);
        }
    }
}
